<template>
  <a-modal v-model="visible" centered @cancel="handleCancel" width="900px">
    <f-print tableShow ref="easyPrint" v-if="printType==1" :beforePrint="beforePrint">
      <template slot-scope="func">
        <order-print :tableData="printData" :getChineseNumber="func.getChineseNumber"></order-print>
      </template>
    </f-print>
    <f-print tableShow ref="easyPrint" v-if="printType==2" :beforePrint="beforePrint">
      <template slot-scope="func">
        <payment-print :tableData="printData" :getChineseNumber="func.getChineseNumber"></payment-print>
      </template>
    </f-print>
    <f-print tableShow ref="easyPrint" v-if="printType==3" :beforePrint="beforePrint">
      <template slot-scope="func">
        <invoice-print :tableData="printData" :getChineseNumber="func.getChineseNumber"></invoice-print>
      </template>
    </f-print>
    <template slot="footer">
      <a-button @click="printSub" v-if="printTag==0&&showPrint==1">打印</a-button>
      <a-button @click="printSub" v-if="printTag==1&&showPrint==1">已打印</a-button>
      <a-button v-if="showPrint==0">禁止打印</a-button>
    </template>
  </a-modal>
</template>

<script>
  import OrderPrint from './order_print'
  import PaymentPrint from './payment_print'
  import InvoicePrint from './invoice_print'
  import api from './../js/api'

  export default {
    name: 'PrintModal',
    components: { InvoicePrint, PaymentPrint, OrderPrint },
    props: {},
    data() {
      return {
        api: Object.assign(api, this.api),
        visible: false,
        printTag: 0,
        showPrint: 0,
        printData: {},
        params: {},
        printType: 0
      }
    },
    created() {

    },
    methods: {
      handleCancel() {
        this.visible = false
        this.printTag = 0
      },
      printSub() {
        this.$refs.easyPrint.print()
        this.visible = false
      },
      beforePrint() {
        const url = this.params.url
        this.http.post(this, {
          url: url,
          data: this.params
        })
      },
      open(businessKey, printType, amountType) {
        let params = {}
        let url = ''
        if (printType == 1) {
          params.orderId = businessKey
          url = this.api.busOrder.getPrintInfo
          params.url = this.api.busOrder.updatePrintTag
        }
        if (printType == 2) {
          params.paymentId = businessKey
          params.amountType = amountType
          url = this.api.payment.getPrintInfo
          params.url = this.api.payment.updatePrintTag
        }
        if (printType == 3) {
          params.paymentId = businessKey
          params.amountType = amountType
          url = this.api.payment.getPrintInfo
          params.url = this.api.payment.updatePrintTag
        }
        this.params = params
        this.http.get(this, {
          url: url,
          params: params,
          success: function(me, data) {
            me.$nextTick(() => {
              me.printData = data
              me.printType = printType
              if (data.printTag == 1) {
                me.printTag = data.printTag
              }
              if (printType == 1) {
                if (data.deliveryValue == '全款') {
                  if (data.nickName_cwbz) {
                    me.showPrint = 1
                  }
                }else{
                  if (data.nickName_zjl) {
                    me.showPrint = 1
                  }
                }
              }else{
                if (data.nickName_zjl) {
                  me.showPrint = 1
                }
              }
              me.visible = true
            })
          }
        })
      }
    }
  }
</script>
<style scoped media="print">
  @page {
    size: auto;
    margin: 1mm;
  }

  html {
    background-color: #ffffff;
    margin: 0px;
  }

  body {
    border: solid 1px #ffffff;
    margin: 0
  }
</style>

